package com.choosefine.base.common.bean

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import java.io.IOException
import java.util.regex.Pattern

/**
 * Created by 邱鹏 on 2017/6/10.
 */
open class BaseToolkitBean {
    @Autowired
    lateinit var jsonObjectMapper: ObjectMapper

    fun match(p: String, str: String): String {
        val pattern = Pattern.compile(p)
        val m = pattern.matcher(str)
        var result = ""
        if (m.find()) {
            result = m.group(1)
        }
        return result
    }

    @Throws(IOException::class)
    fun <T> readJson(jsonStr: String, collectionClass: Class<*>, vararg elementClasses: Class<*>): T {
        val javaType = jsonObjectMapper.getTypeFactory().constructParametricType(collectionClass, *elementClasses)
        return jsonObjectMapper.readValue(jsonStr, javaType)
    }


}